﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;
using LUStationery.Model;
using LUStationery.DAO;

namespace LUStationery.Controller
{
    public class SupplierControl
    {

        public SupplierList getSuppliers()
        {
            SupplierDAO sDAO = new SupplierDAO();
            IDataReader idr = sDAO.getSupplier();
            return SupplierControl.convertIdrToSupplierList(idr);
        }
        public Supplier getSupplierBySupplierName(string supplier)
        {
            SupplierDAO sDAO = new SupplierDAO();
            IDataReader idr = sDAO.getSupplierBySupplierName(supplier);
            return SupplierControl.convertIdrToSupplier(idr);
        }

        //ZHONG YUE\\
        public SupplierItem getSupplierItemBySupplierItem(string itemCode, string supplier)
        {
            SupplierItemDAO siDAO = new SupplierItemDAO();
            IDataReader idr = siDAO.getSupplierItem(itemCode, supplier);
            SupplierItem si = SupplierControl.convertIdrToSupplierItem(idr);
            return si;
        }


        protected static Supplier convertIdrToSupplier(IDataReader idr)
        {
            Supplier s = new Supplier();
            while (idr.Read())
            {
                s.SupplierCode = Convert.ToString(idr["supplier_code"]);
                s.SupplierName = Convert.ToString(idr["supplier_name"]);
            }
            return s;
        }

        protected static SupplierList convertIdrToSupplierList(IDataReader idr)
        {
            SupplierList sList = new SupplierList();
            while (idr.Read())
            {
                Supplier s = new Supplier();
                s.SupplierCode = Convert.ToString(idr["supplier_code"]);
                s.SupplierName = Convert.ToString(idr["supplier_name"]);
                sList.Add(s);
            }
            return sList;
        }


        protected static SupplierItemList convertIdrToSupplierItemList(IDataReader idr)
        {
            SupplierItemList siList = new SupplierItemList();
            while (idr.Read())
            {
                SupplierItem si = new SupplierItem();
                si.SupplierItemID = Convert.ToInt32(idr["supplier_item_id"]);
                si.ItemCode = Convert.ToString(idr["item_code"]);
                si.ItemDesc = Convert.ToString(idr["item_description"]);
                si.SupplierCode = Convert.ToString(idr["supplier_code"]);
                si.SupplierName = Convert.ToString(idr["supplier_name"]);
                si.Price = Convert.ToDouble(idr["price"]);
                siList.Add(si);
            }
            return siList;
        }

        protected static SupplierItem convertIdrToSupplierItem(IDataReader idr)
        {
            SupplierItem si = new SupplierItem();
            while (idr.Read())
            {
                si.SupplierItemID = Convert.ToInt32(idr["supplier_item_id"]);
                si.ItemCode = Convert.ToString(idr["item_code"]);
                si.ItemDesc = Convert.ToString(idr["item_description"]);
                si.SupplierCode = Convert.ToString(idr["supplier_code"]);
                si.SupplierName = Convert.ToString(idr["supplier_name"]);
                si.Price = Convert.ToDouble(idr["price"]);
            }
            return si;
        }
    }
}
